package Hibernate.dao;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Examiee entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see Hibernate.dao.Examiee
 * @author MyEclipse Persistence Tools
 */
public class ExamieeDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(ExamieeDAO.class);
	// property constants
	public static final String ID_CARD_NUM = "idCardNum";
	public static final String ADMIT_ID = "admitId";
	public static final String USER_NAME = "userName";
	public static final String USER_PHOTO = "userPhoto";
	public static final String USER_DESCRIBE = "userDescribe";
	public static final String PHONE_NUM = "phoneNum";
	public static final String MAIL = "mail";
	public static final String SUBJECT = "subject";
	public static final String TEMP_EXAM_ID = "tempExamId";
	public static final String PSD = "psd";
	public static final String STATE = "state";
	public static final String BIRTHDAY = "birthday";
	public static final String POLICY = "policy";
	public static final String NATION = "nation";
	public static final String EE_ID = "eeId";
	public static final String GENDER = "gender";
	public static final String CLASSROOM_ID = "classroomId";

	public void save(Examiee transientInstance) {
		log.debug("saving Examiee instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().save(transientInstance);
			tr.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public void delete(Examiee persistentInstance) {
		log.debug("deleting Examiee instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().delete(persistentInstance);
			tr.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Examiee findById(java.lang.String id) {
		log.debug("getting Examiee instance with id: " + id);
		Transaction tr = getSession().beginTransaction();
		try {
			Examiee instance = (Examiee) getSession().get(
					"Hibernate.dao.Examiee", id);
			tr.commit();
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByExample(Examiee instance) {
		log.debug("finding Examiee instance by example");
		Transaction tr = getSession().beginTransaction();
		try {
			List results = getSession().createCriteria("Hibernate.dao.Examiee")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			tr.commit();
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Examiee instance with property: " + propertyName
				+ ", value: " + value);
		Transaction tr = getSession().beginTransaction();
		try {
			String queryString = "from Examiee as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public List findByIdCardNum(Object idCardNum) {
		return findByProperty(ID_CARD_NUM, idCardNum);
	}

	public List findByAdmitId(Object admitId) {
		return findByProperty(ADMIT_ID, admitId);
	}

	public List findByUserName(Object userName) {
		return findByProperty(USER_NAME, userName);
	}

	public List findByUserPhoto(Object userPhoto) {
		return findByProperty(USER_PHOTO, userPhoto);
	}

	public List findByUserDescribe(Object userDescribe) {
		return findByProperty(USER_DESCRIBE, userDescribe);
	}

	public List findByPhoneNum(Object phoneNum) {
		return findByProperty(PHONE_NUM, phoneNum);
	}

	public List findByMail(Object mail) {
		return findByProperty(MAIL, mail);
	}

	public List findBySubject(Object subject) {
		return findByProperty(SUBJECT, subject);
	}

	public List findByTempExamId(Object tempExamId) {
		return findByProperty(TEMP_EXAM_ID, tempExamId);
	}

	public List findByPsd(Object psd) {
		return findByProperty(PSD, psd);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByBirthday(Object birthday) {
		return findByProperty(BIRTHDAY, birthday);
	}

	public List findByPolicy(Object policy) {
		return findByProperty(POLICY, policy);
	}

	public List findByNation(Object nation) {
		return findByProperty(NATION, nation);
	}

	public List findByEeId(Object eeId) {
		return findByProperty(EE_ID, eeId);
	}

	public List findByGender(Object gender) {
		return findByProperty(GENDER, gender);
	}

	public List findByClassroomId(Object classroomId) {
		return findByProperty(CLASSROOM_ID, classroomId);
	}

	public List findAll() {
		log.debug("finding all Examiee instances");
		Transaction tr = getSession().beginTransaction();
		try {
			String queryString = "from Examiee";
			Query queryObject = getSession().createQuery(queryString);
			tr.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			getSession().flush();
			getSession().close();
		}
	}

	public Examiee merge(Examiee detachedInstance) {
		log.debug("merging Examiee instance");
		try {
			Examiee result = (Examiee) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Examiee instance) {
		log.debug("attaching dirty Examiee instance");
		Transaction tr = getSession().beginTransaction();
		try {
			getSession().saveOrUpdate(instance);
			tr.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Examiee instance) {
		log.debug("attaching clean Examiee instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}